package com.example.boxugu.myapplication.Fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.boxugu.myapplication.Moder.CourseModer;
import com.example.boxugu.myapplication.Moder.Course_bannerModer;
import com.example.boxugu.myapplication.Moder.LoginModer;
import com.example.boxugu.myapplication.Moder.PracticeModer;
import com.example.boxugu.myapplication.R;
import com.example.boxugu.myapplication.activity.Coursedetail_Activity;
import com.example.boxugu.myapplication.banner.GlideImageLoade;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.net.URI;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CourseFragment extends Fragment {
    LinearLayout mdotLayout;
    ViewPager viewPager;
    ArrayList<ImageView> imageViews=new ArrayList<>();
    ArrayList<Course_bannerModer.Data> banner=new ArrayList<>();
    ArrayList<CourseModer.Data> list=new ArrayList<>();
    MyViewPager myViewPager;
    RecyclerView mRecycleview;
    RecycleAdapter recycleAdapter;
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.i("CourseFragment","handler msg");
            handler.sendEmptyMessageDelayed(1,2000);
            viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
        }
    };
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_course, container, false);
        mdotLayout=view.findViewById(R.id.dotLayout);
        viewPager = view.findViewById(R.id.viewpager);
        myViewPager = new MyViewPager();
        mRecycleview=view.findViewById(R.id.recycleview);
        recycleAdapter=new RecycleAdapter();

        mRecycleview.setLayoutManager(new GridLayoutManager(getActivity(),2));
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }
            @Override
            public void onPageSelected(int i) {
                if (i == 0) {
                    viewPager.setCurrentItem(banner.size()-2, false);

                }
                if (i == banner.size()-1) {
                    viewPager.setCurrentItem(1,false);

                }
                 setDots();

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        RequestParams Params = new RequestParams("http://148.70.46.9:8080/boxuegu/bxg/banner");
        x.http().post(Params, new Callback.CacheCallback<String>() {
            @SuppressLint("ResourceType")
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                Course_bannerModer course_bannerModer = gson.fromJson(result, Course_bannerModer.class);
                if (course_bannerModer!=null&&course_bannerModer.data!=null){
                    banner.addAll(course_bannerModer.data);
                    banner.add(course_bannerModer.data.get(0));
                    banner.add(0,course_bannerModer.data.get(course_bannerModer.data.size()-1));
                    viewPager.setAdapter(myViewPager);
                    viewPager.setCurrentItem(1);
                    Log.i("CourseFragment","开始发送");
                handler.sendEmptyMessageDelayed(1,2000);
                for (int i=0;i<=course_bannerModer.data.size();i++){
                        ImageView imageView=new ImageView(getActivity());
                        imageView.setBackgroundResource(R.drawable.course_bar_icon);
                    LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(30,30);
                    params.leftMargin=10;
                    params.rightMargin=10;
                    imageView.setLayoutParams(params);
                    mdotLayout.addView(imageView);
                    imageViews.add(imageView);
                }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }

            @Override
            public boolean onCache(String result) {
                return false;
            }
        });

    
        RequestParams requestParams=new RequestParams("http://148.70.46.9:8080/boxuegu/bxg/chapterlist");
        x.http().post(requestParams, new Callback.CacheCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.i("CourseFragment",result);
                Gson gson=new Gson();
                CourseModer courseModer=gson.fromJson(result,CourseModer.class);
                if (courseModer!=null &&courseModer.data!=null){
                    list=courseModer.data;
                    mRecycleview.setAdapter(recycleAdapter);
              }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i("CourseFragment",ex.getMessage());

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }

            @Override
            public boolean onCache(String result) {
                return false;
            }
        });
        return view;
    }
    public void setDots(){
        for (int i=0;i<imageViews.size();i++){
            if (i==viewPager.getCurrentItem()-1){
                imageViews.get(i).setSelected(true);

            }else {
                imageViews.get(i).setSelected(false);
            }

        }

    }


   public class MyViewPager extends PagerAdapter{

       @Override
       public int getCount() {
           return banner.size();
       }

       @Override
       public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
           return view ==o;
       }

       @Override
       public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
           container.removeView((View)object);
       }

       @NonNull
       @Override
       public Object instantiateItem(@NonNull ViewGroup container, int position) {
           ImageView imageView=new ImageView(getActivity());
           container.addView(imageView);
           x.image().bind(imageView,banner.get(position).bannerimg);
           return imageView;
       }
   }
   public class RecycleAdapter extends RecyclerView.Adapter<RecycleViewHolder>{

       @NonNull
       @Override
       public RecycleViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
           final CourseModer.Data data=list.get(i);
           View view=View.inflate(getActivity(),R.layout.course_listview,null);
           RecycleViewHolder recycleViewHolder=new RecycleViewHolder(view);
           return recycleViewHolder;
       }

       @Override
       public void onBindViewHolder(@NonNull RecycleViewHolder myViewHoder, final int i) {
           myViewHoder.mtitle.setText(list.get(i).title);
           myViewHoder.mSubtitle.setText(list.get(i).subtitle);
           x.image().bind(myViewHoder.mimg,list.get(i).img);
           myViewHoder.mimg.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   Intent intent=new Intent(getActivity(), Coursedetail_Activity.class);
                   intent.putExtra("id",list.get(i).id);
                   startActivity(intent);
               }
           });

       }

       @Override
       public int getItemCount() {
           return list.size();
       }
   }

   public class RecycleViewHolder extends RecyclerView.ViewHolder{
        public TextView mtitle;
        public ImageView mimg;
        public TextView mSubtitle;

       public RecycleViewHolder(@NonNull View itemView) {
           super(itemView);
           mtitle=itemView.findViewById(R.id.title);
           mimg=itemView.findViewById(R.id.img);
           mSubtitle=itemView.findViewById(R.id.subtitle);
       }
   }
}